package com.pptree.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.annotation.WebInitParam;

/**
 * Servlet Filter implementation class EncodingFilter
 */
@WebFilter(urlPatterns={"*.action"},initParams={
		@WebInitParam(name="encoding",value="utf-8")})
public class EncodingFilter implements Filter {
	private FilterConfig config = null;
	private String encoding = "utf-8";
    /**
     * Default constructor. 
     */
    public EncodingFilter() {
        // TODO Auto-generated constructor stub
    	
    	
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		this.config = null;
		this.encoding = null;
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here
			System.out.println("---->进入EncodingFilter的doFilter()方法");
		
			request.setCharacterEncoding(this.encoding);
			response.setContentType("text/html;charset="+this.encoding);
			
		String categoryName = request.getParameter("categoryname");
		System.out.println("收到客户端请求的信息为：" + categoryName); 
		
		// 放行 !! 到达下一个过滤器或目标资源
		chain.doFilter(request, response);
		
		
		
		
		
		System.out.println("---->退出EncodingFilter的doFilter()方法");
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
		this.config = fConfig;
		System.out.println("this.encoding=====>" + this.encoding);
		System.out.println("this.config====>" + this.config.getClass().getCanonicalName());
		this.encoding = this.config.getInitParameter("encoding");
	}

}
